/**********************************************************
 * AnnouncementsManagerAction.java
 *
 * Created by Ashutosh
 * Last modified Date: Aug 25, 2008 11:13:40 AM by Ashutosh
 * Revision: <svn revision #>
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 **********************************************************/

package com.printeverywhere.action;

import static com.printeverywhere.util.MessageConstants.MESSAGE_CANNOT_BE_NULL;
import static com.printeverywhere.util.MessageConstants.SUBJECT_CANNOT_BE_NULL;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.joda.time.DateTime;

import com.printeverywhere.ImpComparator.SortAnnouncement;
import com.printeverywhere.delegator.AnnouncementServiceDelegator;
import com.printeverywhere.dto.Announcement;
import com.printeverywhere.util.DataValidator;
import com.printeverywhere.util.DateUtil;
import com.printeverywhere.util.Sort;

// TODO: Auto-generated Javadoc
/**
 * The Class AnnouncementsManagerAction.
 */
public class AnnouncementsManagerAction extends AdminEngineManagement implements ServletRequestAware{

	/** Logger for this class. */
	private static final Logger logger = Logger.getLogger(AnnouncementsManagerAction.class);
	private Date date;

	/** The time. */
	private Date time;

	private String id;
	private HttpServletRequest request;
	/**
	 * Gets the all announcement.
	 *
	 * @return the all announcement
	 *
	 * @throws Exception the exception
	 */
	public String getAllAnnouncement() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("getAllAnnouncement() - start"); //$NON-NLS-1$
		}
		try{
			announcementTempList = AnnouncementServiceDelegator.getList();
			for (Iterator<Announcement> iterator = announcementTempList.iterator(); iterator.hasNext();) {
				Announcement type = (Announcement) iterator.next();
				if(type.getDate().after(DateUtil.getTodaysDate())){
					type.setActive(true);
				}
				announcementList.add(type);
			}
			Sort.byAnnouncementDate(announcementList);
			if(request.getSession().getAttribute("messages") != null) {
				setActionMessages((Collection)request.getSession().getAttribute("messages"));
				request.getSession().setAttribute("messages", null);
			}
		}catch (Exception e) {
			addActionError("Error in showing announcements.");
			return INPUT;
		}



		if (logger.isDebugEnabled()) {
			logger.debug("getAllAnnouncement() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	/**
	 * Copy announcements manager.
	 *
	 * @return the string
	 *
	 * @throws Exception the exception
	 */
	public String copyAnnouncementsManager() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("copyAnnouncementsManager() - start"); //$NON-NLS-1$
		}
		try{
			if(id!=null && Integer.parseInt(id) >0){
				announcement = AnnouncementServiceDelegator.getAnnouncement(Integer.parseInt(id));
				date = announcement.getDate();
				time = announcement.getDate();
				announcement.setId(null);
			}
		}catch (Exception e) {
			addActionError("Announcement not copied.");
			return INPUT;
		}
		if (logger.isDebugEnabled()) {
			logger.debug("copyAnnouncementsManager() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	/**
	 * Insert or update.
	 *
	 * @return the string
	 *
	 * @throws Exception the exception
	 */
	public String insertOrUpdate() throws Exception {
		try{
			setActionMessages(null);
			if(DataValidator.isNull(announcement.getSubject()))addActionError(SUBJECT_CANNOT_BE_NULL);
			if(!getActionErrors().isEmpty()){
				if (logger.isDebugEnabled()) {
					logger.debug("save() - end"); //$NON-NLS-1$
				}
				return INPUT;
			}
			DateTime date = new DateTime(this.date);
			DateTime time = new DateTime(this.time);
			date = date.plusHours(time.getHourOfDay()).plusMinutes(time.getMinuteOfHour());
			Date todaysDate = new Date();
			logger.info(todaysDate);
			logger.info(date.toDate());
			logger.info(date.toDate().before(todaysDate));//Thu Dec 18 10:15:00 EST 2008 date.compareTo(todaysDate) < 0
			if(date.toDate().before(todaysDate)){
				addActionError("Enter Future date/time.".intern());
				return INPUT;
			}
			announcement.setDate(date.toDate());
			if(announcement.getId() !=null && announcement.getId() >0){
				AnnouncementServiceDelegator.updateAnnouncement(announcement);
				addActionMessage("Announcement successfully updated.");
			}else{
				AnnouncementServiceDelegator.insertAnnouncement(announcement);
				addActionMessage("Announcement successfully added.");
			}
			request.getSession().setAttribute("messages", getActionMessages());
		}catch (Exception e) {
			addActionError("Error in announcement manager.");
			announcementList = AnnouncementServiceDelegator.getList();
			return INPUT;
		}
		return SUCCESS;
	}

	public String showAnnouncement(){
		if (logger.isDebugEnabled()) {
			logger.debug("showAnnouncement() - start"); //$NON-NLS-1$
		}
		try{
			if(id!=null && Integer.parseInt(id) >0){
				announcement = AnnouncementServiceDelegator.getAnnouncement(Integer.parseInt(id));
				date = announcement.getDate();
				time = announcement.getDate();
			}
		}catch (NumberFormatException e) {
			return "listing";
		}catch (Exception e) {
			return INPUT;
		}
		if (logger.isDebugEnabled()) {
			logger.debug("showAnnouncement() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	/**
	 * Delete employee.
	 *
	 * @return the string
	 *
	 * @throws Exception the exception
	 */
	public String deleteAnnouncement() throws Exception {
		setActionMessages(null);
		if (logger.isDebugEnabled()) {
			logger.debug("delete() - start"); //$NON-NLS-1$
		}
		try{
			announcement = AnnouncementServiceDelegator.getAnnouncement(Integer.parseInt(id));
			AnnouncementServiceDelegator.deleteAnnouncement(announcement);
			addActionMessage("Announcement successfully deleted.");
			request.getSession().setAttribute("messages", getActionMessages());
		}catch (Exception e) {
			addActionError("Announcement not deleted.");
			announcementList = AnnouncementServiceDelegator.getList();
			return INPUT;
		}

		if (logger.isDebugEnabled()) {
			logger.debug("delete() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	/** The announcement list. */
	private List<Announcement> announcementList = new ArrayList<Announcement>();
	private List<Announcement> announcementTempList;
	private Announcement announcement;

	/**
	 * Gets the announcement list.
	 *
	 * @return the announcement list
	 */
	public List<Announcement> getAnnouncementList() {
		return announcementList;
	}

	/**
	 * Sets the announcement list.
	 *
	 * @param announcementList the new announcement list
	 */
	public void setAnnouncementList(List<Announcement> announcementList) {
		this.announcementList = announcementList;
	}

	public Announcement getAnnouncement() {
		return announcement;
	}

	public void setAnnouncement(Announcement announcement) {
		this.announcement = announcement;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public Date getTime() {
		return time;
	}

	public void setTime(Date time) {
		this.time = time;
	}

	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

}